package com.joesem.sale.client.logic.workOrder.viewModel

import android.app.Application
import android.os.Build
import android.util.Log
import com.google.gson.Gson
import com.joesem.sale.client.base.BaseViewModel
import com.joesem.sale.client.base.request
import com.joesem.sale.client.base.toast
import com.joesem.sale.client.network.data.OrderDetailsMode
import com.joesem.sale.client.network.data.UploadImgMode
import java.io.File

class OrderCreateViewModel(application: Application) : BaseViewModel(application) {
    fun uploadImg(img: File) {
        request(isLoading = true, request = {
            uploadImg(img)
        }, responseFailed = {
            toast(it.message)
        }, response = { it ->
            it.data?.let {
                actionIntent(ImgUploadIntent(it))
            } ?: toast("上传失败 ${it.msg.orEmpty()}")
        })
    }

    fun confirmOrder(values: HashMap<String, Any?>) {
        request(isLoading = true, request = {
            createWorkOrder(values)
        }, response = {
            toast(it.msg)
            actionIntent(OrderCreateIntent(it.data?.id.orEmpty()))
        }, responseFailed = {
            toast(it.message)
        })
    }

    fun orderAllot(id: Int, values: HashMap<String, Any?>) {
        request(isLoading = true, request = {
            this.orderAllot(id, values)
        }, response = { it ->
            toast(it.msg)
            it.data?.let {
                actionIntent(OrderAllotIntent(it))
            }
        }, responseFailed = {
            toast(it.message)
        })
    }

    class ImgUploadIntent(val mode: UploadImgMode)
    class OrderCreateIntent(val orderId: String)
    class OrderAllotIntent(val result: OrderDetailsMode)
}